<?php
		class User extends MY_Controller{	
			function User() {
				parent::__construct();
				@session_start();
				$this->mod_ad->is_logged_in();
				$_SESSION['redirect'] = $this->uri->uri_string();
			}

			function index(){
				$data['vl_sort'] = "DESC";	
				$data['title'] = "Tài khoản người dùng";
				$data['table'] = 'user';
				$data['controller'] = 'user';
				$data['content_admin'] = 'back_end/user/index';
				$data['sort'] = 'ASC';
				$this->db->order_by('parent_id','asc');
				
				//lay ra cac danh muc cap 1
				$data['category_admin_1'] = $this->db->get_where('admin_menu',array('level' => 1))->result_array();
				//lay ra cac danh muc cap 2
				$data['category_admin_2'] = $this->db->get('admin_menu',array('level' => 2))->result_array();
				//lay ra category hien tai	
				$uri_value = $this->uri->segment(2);
				$this->db->select('root_path,id');
				$data['category_admin_active'] = $this->db->get_where('admin_menu',array('controller' => $uri_value))->row_array();
					
				// lay ra danh muc quan tri
				$this->db->order_by('parent_id','asc');
				$data['category_admin'] = $this->db->get('admin_menu')->result_array();
				
				// lay cac cac user va phan trang
				$per_page = 15;
				$uri = 4;
				$data['admin_user'] = $this->mod_lib->load_all('user','',array(),$per_page,$this->uri->segment($uri),array("id"=>"desc"));																													
				$config['base_url']     =  site_url('back_end/user/index/');
				$config['total_rows']   =  $this->db->count_all('user');
				$config['per_page']     =  $per_page; 
				$config['uri_segment']  =  $uri; 
				$this->pagination->initialize($config);
				
				$this->load->view('back_end/template_backend',$data);	
			}
			
			function search () {
				$data['vl_sort'] = "DESC";	
				$check_search = $this->input->POST('formSearch');
				$per_page = 10;
				$data['title'] = "Tìm kiếm thành viên";
				$data['table'] = 'user';
				$data['controller'] = 'user';
				$data['content_admin'] = 'back_end/user/index';
				$data['sort'] = 'ASC';
				$this->db->order_by('parent_id','asc');
	
				//lay ra cac danh muc cap 1
				$data['category_admin_1'] = $this->db->get_where('admin_menu',array('level' => 1))->result_array();
				//lay ra cac danh muc cap 2
				$data['category_admin_2'] = $this->db->get('admin_menu',array('level' => 2))->result_array();
				//lay ra category hien tai	
				$uri_value = $this->uri->segment(2);
				$this->db->select('root_path,id');
				$data['category_admin_active'] = $this->db->get_where('admin_menu',array('controller' => $uri_value))->row_array();
				
				if(!empty($check_search)){
					$_SESSION['like_user'] = array('user_name' =>$this->input->POST('nameSearch'));																												
				} else {
					$_SESSION['like_user'] = array();
				}
				
				$data_search = $this->mod_lib->search('user','',$_SESSION['like_user'],array(),array("id"=>"desc"));
				$data['admin_user'] = $this->mod_lib->load_search('user','',$_SESSION['like_user'],array(),$per_page,$this->uri->segment(4),array("id"=>"desc"));		
				$config['base_url']     =  site_url('back_end/user/search/');
				$config['total_rows']   =  count($data_search);
				$config['per_page']     =  $per_page;
				$config['uri_segment']  =  4; 
				$this->pagination->initialize($config);		
				$this->load->view('back_end/template_backend',$data);	
			}
			
			function view ($id) {
				// title 
				$data['title']			= "Hiển thị bình luận";
				
				//lay ra cac danh muc cap 1
				$data['category_admin_1'] = $this->db->get_where('admin_menu',array('level' => 1))->result_array();
				//lay ra cac danh muc cap 2
				$data['category_admin_2'] = $this->db->get('admin_menu',array('level' => 2))->result_array();
				//lay ra category hien tai	
				$uri_value = $this->uri->segment(2);
				$this->db->select('root_path,id');
				$data['category_admin_active'] = $this->db->get_where('admin_menu',array('controller' => $uri_value))->row_array();
				
				// lay ra  1 user	
				$data['admin_user'] = $this->db->get_where('user',array( 'id' => $id ))->row_array();
				$data['content_admin'] = 'back_end/user/view';
				
				// lay ra danh muc quan tri
				$this->db->order_by('parent_id','asc');
				$data['category_admin'] = $this->db->get('admin_menu')->result_array();
				
				$this->load->view('back_end/template_backend',$data);	
			}
			
			function delete() {
				$ids =  implode(",", $this->input->post('value_id'));

				if(!empty($ids)){
					if($this->mod_lib->deleteRecord('user',$ids)){
						$this->session->set_flashdata('msview', 'Xóa thành công !');
					} else {
						$this->session->set_flashdata('msview', 'Xóa thất bại !');
					}
					
					$this->session->set_flashdata('msview', 'Xóa thành công !');
				} else {
					$this->session->set_flashdata('msview', 'Xóa thất bại !');
				}		
				
				redirect('back_end/user');
			}
			
			function sort($field,$sort = '') {
				$per_page = 10;
				$uri = 4;
				$data['title'] = "Sắp xếp thành viên";
				$data['table'] = 'user';
				$data['controller'] = 'user';
				$data['content_admin'] = 'back_end/user/index';
				$this->db->order_by('parent_id','asc');
		
				//lay ra cac danh muc cap 1
				$data['category_admin_1'] = $this->db->get_where('admin_menu',array('level' => 1))->result_array();
				//lay ra cac danh muc cap 2
				$data['category_admin_2'] = $this->db->get('admin_menu',array('level' => 2))->result_array();
				//lay ra category hien tai
				$uri_value = $this->uri->segment(2);
				$this->db->select('root_path,id');
				$data['category_admin_active'] = $this->db->get_where('admin_menu',array('controller' => $uri_value))->row_array();
				
				$uri_value_4 =  $this->uri->segment(4);
				$uri_value_5 =  $this->uri->segment(5);
				
				$_SESSION['order_sort'] = array(
					$uri_value_4 => $uri_value_5 
				);
				
				$_SESSION['sort'] = $sort;
				$_SESSION['field'] = $field;
				
				if ($sort == 'DESC') {
					$data['sort'] =  'ASC';
				} else if($sort == 'ASC') {
					$data['sort'] =  'DESC';
				}
				
				$data_search = $this->mod_lib->search('user','',array(),array(),array("id"=>"desc"));
				$data['admin_user'] = $this->mod_lib->load_search('user','',array(),array(),$per_page,$this->uri->segment($uri),array($_SESSION['field']=>$_SESSION['sort']));		
				$config['base_url']     =  site_url('back_end/user/index');
	
				$config['total_rows']   =  count($data_search);
				$config['per_page']     =  $per_page;
				$config['uri_segment']  =  $uri; 
				$this->pagination->initialize($config);		
				$this->load->view('back_end/template_backend',$data);
			}
		
			function deleteId($id) {
				if(!empty($id)){
					if($this->mod_lib->deleteRecord('user',$id)){
						$this->session->set_flashdata('msview', 'Xóa thành công !');
					} else {
						$this->session->set_flashdata('msview', 'Xóa thất bại !');
					}
				
					$this->session->set_flashdata('msview', 'Xóa thành công !');
					} else {
						$this->session->set_flashdata('msview', 'Xóa thất bại !');
					}		
				redirect('back_end/user');	
				}
			}
?>